import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import Welcome from '@/components/Welcome.vue'
import Users from './components/user/users.vue'
import Rights from './components/user/Right.vue'
import Roles from './components/user/Roles.vue'
import Cate  from './components/goods/Cate.vue'
import Params from './components/goods/Params.vue'
import Goods from './components/goods/Goods.vue'
import Add from './components/goods/Add.vue'


Vue.use(Router)
const router=new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path:'/home',component:Home,children:[
      { path: '/home', redirect: '/welcome' },
      // 子路由重定向  另外子路由在写的时候直接写/+子路由即可 不需要嵌套父路由
      {path:'/welcome',component:Welcome},
      {path:'/users',component:Users},
      {path:'/rights',component:Rights},
      {path:'/roles',component:Roles},
      {path:'/categories',component:Cate},
      {path:'/params',component:Params},
      {path:'/goods',component:Goods},
      {path:'/goods/add',component:Add}
    ]}
  ]
})
router.beforeEach((to,form,next)=>{
  //如果用户访问的是/login则直接跳转
  if(to.path=='/login') return next();
  // 如果不是则需要去获取token
  //如果没有token存在则直接将用户跳转到、login页面
  if(!sessionStorage.getItem('token')) {
  return next('/login') //如果不存在则让用户跳转会login
  }else {
    next() //如果有token则可以正常访问
  }
})
export default router
